package com.mlx.mbg.code.method;

import com.mlx.mbg.code.MbgUtil;
import com.mlx.mbg.code.inter.EnableMethod;
import com.mlx.mbg.code.config.MbgPropertyConfig;
import com.mlx.mbg.code.inter.ExtensionLevel;
import com.mlx.mbg.code.inter.IExtensionMethod;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.Collections;
import java.util.List;

@EnableMethod
public class DeleteByIdCollectionMethod  implements IExtensionMethod{
    @Override
    public boolean checkConfig(ExtensionLevel level, MbgPropertyConfig mbgPropertyConfig, IntrospectedTable introspectedTable) {
        return getExtensionLevelSet().contains(level) && mbgPropertyConfig.getEnableDeleteByIdCollection() && MbgUtil.checkOnePrimaryKey(introspectedTable, "deleteByIdCollection");
    }

    @Override
    public int getOrder() {
        return 700;
    }

    @Override
    public List<Method> buildMethod(IntrospectedTable introspectedTable) {
        List<IntrospectedColumn> primaryKeyColumnList = introspectedTable.getPrimaryKeyColumns();

        FullyQualifiedJavaType primaryKeyType = primaryKeyColumnList.get(0).getFullyQualifiedJavaType();

        // 访问类型：public
        JavaVisibility methodVisibility = JavaVisibility.PUBLIC;

        // 返回类型：int
        FullyQualifiedJavaType returnType = new FullyQualifiedJavaType("int");

        // 方法名称
        String methodName = "deleteByIdCollection";

        // 参数类型
        FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType("Collection");
        parameterType.addTypeArgument(primaryKeyType);
        Parameter parameter = new Parameter(parameterType, "idCollection");

        // 创建方法对象
        Method method = new Method();
        // 设置访问类型
        method.setVisibility(methodVisibility);
        // 设置返回类型对象
        method.setReturnType(returnType);
        // 设置方法名
        method.setName(methodName);
        // 设置方法参数
        method.addParameter(parameter);
        // 设置备注
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * id集合删除");
        method.addJavaDocLine(" */");

        return Collections.singletonList(method);
    }

    @Override
    public List<XmlElement> buildSQL(IntrospectedTable introspectedTable) {
        List<IntrospectedColumn> primaryKeyColumnList = introspectedTable.getPrimaryKeyColumns();

        IntrospectedColumn introspectedColumn = primaryKeyColumnList.get(0);
        String actualColumnName = introspectedColumn.getActualColumnName();
        String jdbcTypeName = introspectedColumn.getJdbcTypeName();
        // 增加base_query
        XmlElement where = new XmlElement("where");
        XmlElement whereChoose = new XmlElement("choose");
        XmlElement whereChooseWhen = new XmlElement("when");
        whereChooseWhen.addAttribute(new Attribute("test", "idCollection == null or idCollection.size == 0"));
        TextElement whereChooseWhenText = new TextElement("and false");
        XmlElement whereChooseOtherwise = new XmlElement("otherwise");
        TextElement text1 = new TextElement("and " + actualColumnName + " in ");
        whereChooseOtherwise.addElement(text1);
        XmlElement whereChooseOtherwiseForeach = new XmlElement("foreach");
        whereChooseOtherwiseForeach.addAttribute(new Attribute("collection", "idCollection"));
        whereChooseOtherwiseForeach.addAttribute(new Attribute("item", "id"));
        whereChooseOtherwiseForeach.addAttribute(new Attribute("open", "("));
        whereChooseOtherwiseForeach.addAttribute(new Attribute("separator", ","));
        whereChooseOtherwiseForeach.addAttribute(new Attribute("close", ")"));


        TextElement text2 = new TextElement("#{id,jdbcType=" + jdbcTypeName + "}");
        whereChooseOtherwiseForeach.addElement(text2);

        whereChooseOtherwise.addElement(whereChooseOtherwiseForeach);
        whereChooseWhen.addElement(whereChooseWhenText);
        whereChoose.addElement(whereChooseWhen);
        whereChoose.addElement(whereChooseOtherwise);
        where.addElement(whereChoose);

        XmlElement deleteByIdCollection = new XmlElement("delete");
        deleteByIdCollection.addAttribute(new Attribute("id", "deleteByIdCollection"));
        TextElement text4 = new TextElement("delete from " + introspectedTable.getFullyQualifiedTableNameAtRuntime());
        deleteByIdCollection.addElement(text4);
        deleteByIdCollection.addElement(where);

        return Collections.singletonList(deleteByIdCollection);
    }
}
